Imports System.Drawing

Public Class frmTableViewer
    Private dgv As WRDB.Controls.DGVEditor

    Private Sub frmTableViewer_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
    End Sub

    Private Sub frmTableViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GetWindowPos(REGAPP, Me)
        dgv = New WRDB.Controls.DGVEditor(dgTable)
        'cannot display blob column properly in grid; add a link column for object type to perform operations
        With dgTable
            For c As Integer = 0 To .Columns.Count - 1
                If .Columns(c).Name = "Object_Type" Then
                    dgv.SetColumnFormat(WRDB.Controls.DGVEditor.enumColumnTypes.Link, c)
                End If
            Next
        End With
    End Sub

    Private Sub dgTable_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgTable.CellContentClick
        'if is displaying blob entry, show context menu
        With dgTable
            If .Columns(e.ColumnIndex).Name = "Object_Type" And .Columns(e.ColumnIndex).GetType Is GetType(DataGridViewLinkColumn) Then
                Dim pt As Point
                With .GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
                    pt = New Point(.Left + .Width / 2, .Top + .Height / 2)
                End With
                ContextMenuObject.Show(dgTable, pt)
            End If
        End With
    End Sub

    Private Sub mnu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDisplay.Click, mnuSaveAs.Click
        With dgTable
            Dim dt As DataTable = dgTable.DataSource
            Dim ext As String = TestNull(.Item("Object_Type", .CurrentRow.Index).Value, "")
            Dim blob() As Byte = dt.Rows(dgTable.CurrentRow.Index)("Object_Value")
            Dim BaseDate As Date = TestNull(.CurrentRow.Cells("Date_Time").Value, Date.MinValue)
            Dim ObjType As clsBlob.enumObjectType = clsBlob.ObjectType(ext)
            Select Case ObjType
                Case clsBlob.enumObjectType.TimeSeries, clsBlob.enumObjectType.ValueSeries : ext = "TXT"
            End Select

            If sender Is mnuDisplay Then
                If ext <> "" Then
                    Dim FileName As String = String.Format("{0}\WRDB{1}.{2}", clsProject.TempPath, .CurrentRow.Index, ext)
                    clsBlob.SaveBlob(ext, FileName, blob, BaseDate)
                    Process.Start(FileName)
                End If

            ElseIf sender Is mnuSaveAs Then
                With New SaveFileDialog
                    .AddExtension = True
                    .DefaultExt = ext
                    .InitialDirectory = GetSetting(REGAPP, Me.Name, "Directory")
                    .OverwritePrompt = True
                    .SupportMultiDottedExtensions = False
                    .Title = "Save Attachment As File"
                    .Filter = String.Format("{0} Files (*.{1})|*.{1}", ext, ext.ToLower)
                    If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                        clsBlob.SaveBlob(ObjType, .FileName, blob, BaseDate)
                        SaveSetting(REGAPP, Me.Name, "Directory", IO.Path.GetDirectoryName(.FileName))
                    End If
                    .Dispose()
                End With
            End If
        End With
    End Sub

    Private Sub ContextMenuObject_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuObject.Opening
        'note: normally viewer is read-only, but for testing can try all menu items (but changes are not saved in table)
        With dgTable
            Dim ext As String = TestNull(.Item("Object_Type", .CurrentRow.Index).Value, "")
            mnuDisplay.Enabled = ext <> ""
            mnuSaveAs.Enabled = ext <> ""
        End With
    End Sub
End Class